package com.leoblog.test.dao;

import com.leoblog.dao.impl.CategoryDao;
import com.leoblog.dao.impl.CommentDao;
import com.leoblog.dao.impl.EntryDao;
import com.leoblog.dao.impl.UserDao;
import com.leoblog.dao.impl.WebSiteDao;
import com.leoblog.domain.Category;
import com.leoblog.domain.Comment;
import com.leoblog.domain.Entry;
import com.leoblog.domain.Tag;
import com.leoblog.domain.User;
import com.leoblog.domain.WebSite;
import com.leoblog.test.CategoryMother;
import com.leoblog.test.CommentMother;
import com.leoblog.test.EntryMother;
import com.leoblog.test.TagMother;
import com.leoblog.test.UserMother;
import com.leoblog.test.WebSiteMother;
import com.leoblog.test.base.AbstractUTestBaseSpringDaoTestCase;

public class UTestCommentDao extends AbstractUTestBaseSpringDaoTestCase{
	
	private CommentDao commentDao;
	private EntryDao entryDao;
	private WebSiteDao webSiteDao;
	private UserDao userDao;
	private CategoryDao categoryDao;
	
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public CategoryDao getCategoryDao() {
		return categoryDao;
	}
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	public CommentDao getCommentDao() {
		return commentDao;
	}
	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}
	public EntryDao getEntryDao() {
		return entryDao;
	}
	public void setEntryDao(EntryDao entryDao) {
		this.entryDao = entryDao;
	}
	public WebSiteDao getWebSiteDao() {
		return webSiteDao;
	}
	public void setWebSiteDao(WebSiteDao webSiteDao) {
		this.webSiteDao = webSiteDao;
	}
	
	public void testAdd()throws Exception{
		Entry entry = this.createEntry();
		entryDao.save(entry);
		
		Comment comment = CommentMother.newInstance();
		comment.setEntry(entry);
		commentDao.save(comment);
		
		assertNotNull(comment.getId());
		//load it from data base
		Comment loadedComment = commentDao.get(comment.getId());
		
		assertNotNull(loadedComment.getEntry());
		
		assertEquals(entry.getId(), loadedComment.getEntry().getId());
		assertEquals("testContent",comment.getContent());
	}
	
	public void testUpdate()throws Exception{
		Entry entry = this.createEntry();
		entryDao.save(entry);
		Comment comment = CommentMother.newInstance();
		comment.setEntry(entry);
		commentDao.save(comment);
		//update it
		comment.setContent("updatedContent");
		//persist
		commentDao.save(comment);
		
		assertEquals("updatedContent", comment.getContent());
	}
	
	private Entry createEntry(){
		User author = UserMother.newInstance();
		
		Category category = CategoryMother.newInstance();
		
		WebSite webSite = WebSiteMother.newInstance();
		
		Tag tag = TagMother.newInstance();
		
		webSite.setUser(author);
		
		userDao.save(author);

		categoryDao.save(category);
		
		webSiteDao.save(webSite);
		
		assertNotNull(author);
		
		assertNotNull(category);
		
		assertNotNull(webSite);
		
		Entry entry = EntryMother.newInstance();
		entry.setAuthor(author);
		entry.setCategory(category);
		entry.setWebSite(webSite);
		entry.getTags().add(tag);
		return entry;
	}

}
